﻿using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using AdventureWorksLT.Views.Customers.ViewModels;
using AdventureWorksLT.Web.Services;

namespace AdventureWorksLT.Views.Customers.ChildWindows
{
    public partial class SalesOrderDetailAddCW : ChildWindow
    {

        readonly int loadedSalesOrderID = AdventureWorksLT.Views.Customers.ViewModels.SalesOrderHeadersViewModel.DashLoad_PK;
        //private int loadedPatient_Id = 1;
        private EntityQuery<AdventureWorksLT.Web.Models.Product> _productQuery;
        private AdventureWorksLTDomainContext _context;
        
        public SalesOrderDetailAddCW()
        {
            InitializeComponent();
        LoadComboBoxData();
            
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
            {
                SalesOrderIDTextBox.Text = loadedSalesOrderID.ToString(); //make sure is not the primary key of the current form, it needs to be a FOREIGN key.
                ProductIDTextBox.Text = ProductIDComboBox.SelectedIndex.ToString();
            if (!SalesOrderDetailsViewModel.SalesOrderDetailsContext.IsLoading)
                {
                    this.SalesOrderDetailDataForm.CommitEdit();
                    this.DialogResult = true;
                }
            }
            
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        
        private void LoadComboBoxData()
        {

            //ProductIDComboBox
             _context = new AdventureWorksLTDomainContext();
            _productQuery = _context.GetProductsQuery();
            _productQuery = _productQuery.OrderBy(e => e.ProductID);
            _context.Load(_productQuery, LoadBehavior.RefreshCurrent, ProductIDComboBox_Loaded, null);
            ProductIDComboBox.ItemsSource = _context.Products;
        }


            private void ProductIDComboBox_Loaded(LoadOperation<AdventureWorksLT.Web.Models.Product> lo)
            {
                this.ProductIDComboBox.SelectedIndex = 1;
            }

            private void SalesOrderDetailDataForm_EditEnded(object sender, DataFormEditEndedEventArgs e)
            {
                this.Close();
            }

    }
}

